class DateUtil {
  static String _addLeadingZero(int number) {
    if (number < 10) {
      return '0$number';
    }
    return number.toString();
  }
  /**
   * yyyy-MM-dd HH:mm:ss.SS
   */

  static String getTime({String? format}) {
    DateTime now = DateTime.now();
    String year = now.year.toString();
    String month = _addLeadingZero(now.month);
    String day = _addLeadingZero(now.day);
    String hour = _addLeadingZero(now.hour);
    String minute = _addLeadingZero(now.minute);
    String second = _addLeadingZero(now.second);
    String millisecond = now.millisecond.toString();
    if (format == null) {
      return '${year}-${month}-${day} '
          '${hour}:${minute}:${second}.${millisecond}';
    }
    return format
        .replaceAll(RegExp(r'yyyy'), year)
        .replaceAll(RegExp(r'MM'), month)
        .replaceAll(RegExp(r'dd'), day)
        .replaceAll(RegExp(r'HH'), hour)
        .replaceAll(RegExp(r'mm'), minute)
        .replaceAll(RegExp(r'ss'), second)
        .replaceAll(RegExp(r'SS'), millisecond);
  }
}
